package opg2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;


public class SendMessage extends Thread{
	
	private Socket socket;
	
	public SendMessage(Socket socket) {
		this.socket = socket;
	}
	
	public void run() {
		if (socket != null) {
		    System.out.println("Connected to " + socket);
		    try {
			// For reading from standard input
			BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
			// For sending text to the server
			PrintWriter toServer = new PrintWriter(socket.getOutputStream(),true);
			String s;
			// Read from standard input and send to server
			// Ctrl-D terminates the connection
			System.out.print("Type something for the server and then RETURN> ");
			while ((s = stdin.readLine()) != null && !toServer.checkError()) { 
			    System.out.print("Type something for the server and then RETURN> ");
			    toServer.println(s);
			}
			socket.close();
		    } catch (IOException e) {
			// We ignore IOExceptions
		    }
		}
	}
	
}
